// 菜单栏、dock 栏菜单设置
const electron = require('electron')
const BrowserWindow = electron.BrowserWindow
const Menu = electron.Menu
const app = electron.app
const url = require('url')
const path = require('path')
let template = [{
    label: "后退",
    click: (item, focusedWindow) => {
      if (focusedWindow) {
        focusedWindow.webContents.executeJavaScript('window.history.go(-1)');
      }
    }
  },
  {
    label: "前进",
    click: (item, focusedWindow) => {
      if (focusedWindow) {
        focusedWindow.webContents.executeJavaScript('window.history.go(1)');
      }
    }
  },
  {
    label: '编辑',
    submenu: [{
      label: '撤销',
      accelerator: 'CmdOrCtrl+Z',
      role: 'undo'
    }, {
      label: '重做',
      accelerator: 'Shift+CmdOrCtrl+Z',
      role: 'redo'
    }, {
      type: 'separator'
    }, {
      label: '剪切',
      accelerator: 'CmdOrCtrl+X',
      role: 'cut'
    }, {
      label: '复制',
      accelerator: 'CmdOrCtrl+C',
      role: 'copy'
    }, {
      label: '粘贴',
      accelerator: 'CmdOrCtrl+V',
      role: 'paste'
    }, {
      label: '全选',
      accelerator: 'CmdOrCtrl+A',
      role: 'selectall'
    }]
  }, {
    label: '查看',
    submenu: [{
      label: '刷新',
      accelerator: 'Alt+Z',
      click: function (item, focusedWindow) {
        if (focusedWindow) {
          if (focusedWindow.id === 1) {
            BrowserWindow.getAllWindows().forEach(function (win) {
              if (win.id > 1) {
                win.close()
              }
            })
          }
          focusedWindow.reload()
        }
      }
    }, {
      label: '查找',
      accelerator: 'Ctrl+F',
      click: function (item, focusedWindow) {
        if (focusedWindow) {
          focusedWindow.webContents.executeJavaScript(`const { remote, ipcRenderer } = require('electron')
          const { FindInPage } = require('electron-find')
          let findInPage = new FindInPage(remote.getCurrentWebContents())
          findInPage.openFindWindow()`);
        }
      }
    }, {
      label: '全屏',
      accelerator: (function () {
        if (process.platform === 'darwin') {
          return 'Ctrl+Command+F'
        } else {
          return 'F11'
        }
      })(),
      click: function (item, focusedWindow) {
        if (focusedWindow) {
          focusedWindow.setFullScreen(!focusedWindow.isFullScreen())
        }
      }
    }, {
      label: '开发者工具',
      accelerator: (function () {
        if (process.platform === 'darwin') {
          return 'Alt+Command+I'
        } else {
          return 'F12'
        }
      })(),
      click: function (item, focusedWindow) {
        if (focusedWindow) {
          focusedWindow.toggleDevTools()
        }
      }
    }]
  }, {
    label: "主页",
    accelerator: 'CommandOrControl+w',
    click(item, focusedWindow) {
      let urls = path.join(__dirname, '/../layui/index.html')
      focusedWindow.loadURL(url.format({
        pathname: urls,
        protocol: 'file:',
        slashes: true
      }))
    },
  }
]

function findReopenMenuItem() {
  const menu = Menu.getApplicationMenu()
  if (!menu) return

  let reopenMenuItem
  menu.items.forEach(function (item) {
    if (item.submenu) {
      item.submenu.items.forEach(function (item) {
        if (item.key === 'reopenMenuItem') {
          reopenMenuItem = item
        }
      })
    }
  })
  return reopenMenuItem
}

if (process.platform === 'win32') {
  const helpMenu = template[template.length - 1].submenu
}

app.on('ready', function () {
  const menu = Menu.buildFromTemplate(template)
  Menu.setApplicationMenu(menu)
})

app.on('browser-window-created', function () {
  let reopenMenuItem = findReopenMenuItem()
  if (reopenMenuItem) reopenMenuItem.enabled = false
})

app.on('window-all-closed', function () {
  let reopenMenuItem = findReopenMenuItem()
  if (reopenMenuItem) reopenMenuItem.enabled = true
})